package com.ruoyi.system.controller;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.MapperUtils;
import com.ruoyi.system.domain.InvoiceDetail;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.VendorInvoicing;
import com.ruoyi.system.service.IVendorInvoicingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物资中心开票Controller
 * 
 * @author ruoyi
 * @date 2024-06-18
 */
@RestController
@RequestMapping("/system/invoicing")
public class VendorInvoicingController extends BaseController
{
    @Autowired
    private IVendorInvoicingService vendorInvoicingService;

    /**
     * 查询物资中心开票列表
     */
    @GetMapping("/list")
    public String list(HttpServletResponse response,VendorInvoicing vendorInvoicing,@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                       @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize)
    {
        List<VendorInvoicing> list = vendorInvoicingService.selectVendorInvoicingList(vendorInvoicing);
        DecimalFormat df = new DecimalFormat("0.00##");
        Double sumMoney = 0.0;
        for (VendorInvoicing vendorInvoicing1:list) {
          sumMoney = Double.valueOf(vendorInvoicing1.getMoeny())+sumMoney;
        }
        response.setHeader("sumMoney",df.format(sumMoney));
        response.setHeader("sumCount",list.size()+"");

        // 对列表进行分页处理
        int total = list.size();
        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);
        List<VendorInvoicing> returnList = list.subList(startIndex, endIndex);
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("code",200);
        hashMap.put("msg","查询成功");
        hashMap.put("data",returnList);
        hashMap.put("total", total);
        return MapperUtils.mapToJson(hashMap);
    }

    /**
     * 导出物资中心开票列表
     */
    @Log(title = "物资中心开票", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VendorInvoicing vendorInvoicing)
    {
        List<VendorInvoicing> list = vendorInvoicingService.selectVendorInvoicingList(vendorInvoicing);
        ExcelUtil<VendorInvoicing> util = new ExcelUtil<VendorInvoicing>(VendorInvoicing.class);
        util.exportExcel(response, list, "物资中心开票数据");
    }

    /**
     * 获取物资中心开票详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(vendorInvoicingService.selectVendorInvoicingById(id));
    }

    /**
     * 新增物资中心开票
     */
    @Log(title = "物资中心开票", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VendorInvoicing vendorInvoicing)
    {
        return toAjax(vendorInvoicingService.insertVendorInvoicing(vendorInvoicing));
    }

    /**
     * 修改物资中心开票
     */
    @Log(title = "物资中心开票", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VendorInvoicing vendorInvoicing)
    {
        return toAjax(vendorInvoicingService.updateVendorInvoicing(vendorInvoicing));
    }

    /**
     * 删除物资中心开票
     */
    @Log(title = "物资中心开票", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(vendorInvoicingService.deleteVendorInvoicingByIds(ids));
    }

    /**
     * 物资中心查看供应商金额
     */
    @GetMapping("/listGroupProvide")
    public TableDataInfo listGroupProvide(@RequestParam("provide")String provide,@RequestParam(value = "notNull",defaultValue = "false")Boolean notNull)
    {
        startPage();
        List<Map<String,Object>> list = vendorInvoicingService.listGroupProvide(provide,notNull);
        return getDataTable(list);
    }
}
